package com.jinzhuo.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinzhuo.music.domain.Collect;
import com.jinzhuo.music.domain.Comment;
import com.jinzhuo.music.service.CollectionService;
import com.jinzhuo.music.service.CommentService;
import com.jinzhuo.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;

    /**
     * 添加收藏
     */
    @RequestMapping(value = "createCollection",method = RequestMethod.POST)
    public Object createCollection(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String userId = request.getParameter("userId");
        String type = request.getParameter("type");
        String songId = request.getParameter("songId");
        if (songId==null||songId.equals("")){
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"收藏歌曲为空");
            return object;
        }
        if (collectionService.existSongId(Integer.parseInt(userId),Integer.parseInt(songId))){
            object.put(Consts.CODE,2);
            object.put(Consts.MSG,"已收藏");
            return object;
        }

        //保存到评价的对象中
        Collect collect = new Collect();
        collect.setUserId(Integer.parseInt(userId));
        collect.setType(new Byte(type));
        collect.setSongId(Integer.parseInt(songId));
//        collect.setCreateTime(new Date());

        boolean flag = collectionService.createCollection(collect);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"收藏成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"收藏失败");
        }
        return object;
    }


    /**
     * 删除收藏(根据用户id和歌曲id)
     */
    @RequestMapping(value = "deleteCollection",method = RequestMethod.POST)
    public Object deleteCollection(HttpServletRequest request) {
        JSONObject object = new JSONObject();
        String userId = request.getParameter("userId");
        String songId = request.getParameter("songId");
        boolean flag = collectionService.deleteCollection(Integer.parseInt(userId),Integer.parseInt(songId));
        return flag;
    }

    /**
     * 查询所有收藏
     */
    @RequestMapping(value = "queryAllCollection",method = RequestMethod.POST)
    public Object queryAllCollection(HttpServletRequest request) {
       return collectionService.queryAllCollection();
    }

    /**
     * 查询某个用户的收藏列表
     */
    @RequestMapping(value = "queryCollectionByUserId",method = RequestMethod.POST)
    public Object queryCollectionByUserId(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        return collectionService.queryCollectionByUserId(Integer.parseInt(userId));
    }

}
